/*
 * Copyright 2007-2020 Ofbase.NET(SZH).  All rights reserved.
 */
package framework.web.rpc.adapter.extjs;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import framework.web.rpc.adapter.JsonAdapter;

/**
 * 
 * @version $Revision$ $Date$
 * @author Base
 * @since 1.0
 */
public class AdapterExtjs extends JsonAdapter {
	/** 数据类型为PLAIN */
	public static final String	TYPE_PLAIN	= "PLAIN:";

	/* (non-Javadoc)
	 * @see framework.web.rpc.adapter.IAdapter#translate(java.lang.Object)
	 */
	@Override
	public String translate(Object o) {
		clear();
		if (o instanceof IJsonResultSet) {
			IJsonResultSet jrs = (IJsonResultSet) o;
			if (jrs.getTotalProperty() > 0)
				put("totalProperty", jrs.getTotalProperty());
			putArray("root", jrs.getRoot());
		} else {
			if (o.toString().startsWith(TYPE_PLAIN))
				return o.toString().substring(TYPE_PLAIN.length());
			else
				put("result", o);
		}
		return toJsonString();
	}

	public static void main(String[] args) {
		Map<String, Object> m = new HashMap<String, Object>();
		for (int i = 0; i < 4; i++)
			m.put("t1" + i, "v1" + i);
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		list.add(m);

		Map<String, Object> m2 = new HashMap<String, Object>();
		for (int i = 0; i < 4; i++)
			m2.put("t2" + i, "v2" + i);
		list.add(m2);

		AdapterExtjs out = new AdapterExtjs();
		System.out.println(out.toJsonString(m));

		out.putArray("AA", list);
		System.out.println(out.toString());
	}
}
